﻿using Kz.Telegram.Bot.Console.HandleUpdate.Enums;
using Kz.Telegram.Bot.Console.HandleUpdate.Models;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;

namespace Kz.Telegram.Bot.Console.HandleUpdate.MessageTypeHandler.MessageText
{
    public class MessageTextUrlHandler : IHandler
    {
        public async Task Handle(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
        {

            //!! 管理员或群主，无法被禁言 !!

            var msg = update.Message!;

            //测试 判断是否禁止发送url消息

            //可入库处理查询，不需要每条信息调用BotAPI
            var admins = await botClient.GetChatAdministratorsAsync(msg.Chat.Id, cancellationToken);
            if (admins.Any(w => w.User.Id == msg.From!.Id))
            {
                return;
            }

            var limitDate = DateTime.Now.AddMinutes(10);

            await botClient.RestrictChatMemberAsync(msg.Chat.Id, msg.From!.Id, new ChatPermissions()
            {
                CanAddWebPagePreviews = false,
                CanSendMessages = false,
                CanSendAudios = false,
                CanSendVideos = false,
                CanChangeInfo = false,
                CanInviteUsers = false,
                CanManageTopics = false,
                CanPinMessages = false,
                CanSendDocuments = false,
                CanSendOtherMessages = false,
                CanSendPhotos = false,
                CanSendPolls = false,
                CanSendVideoNotes = false,
                CanSendVoiceNotes = false,
            },
            useIndependentChatPermissions: true,
            untilDate: limitDate,
            cancellationToken: cancellationToken);

            //删除违禁消息
            try
            {
                await botClient.DeleteMessageAsync(msg.Chat.Id, msg.MessageId, cancellationToken: cancellationToken);
            }
            catch (Exception)
            {

            }

            //发送提醒信息
            await botClient.SendTextMessageAsync(
                chatId: msg.Chat.Id,
                text: $"<a href='tg://user?id={msg.From.Id}'>{msg.From!.FirstName}{msg.From.LastName}</a>发送了未经授权的 🔗 网页链接。\r\n• 处理：已禁言 🔇  到 {limitDate:yyyy-MM-dd HH:mm:ss}",
                parseMode: ParseMode.Html,
                replyMarkup: new InlineKeyboardMarkup(InlineKeyboardButton.WithCallbackData(text: "✅ 解禁(管理员)", callbackData: CallbackQueryModel.Create(InlineCallbackEnum.Unmute, msg.From.Id.ToString()))),
                cancellationToken: cancellationToken);


            //return;

            ////等待一下
            //await Task.Delay(5 * 1000, cancellationToken);
            ////测试解除禁言
            //await botClient.RestrictChatMemberAsync(msg.Chat.Id, msg.From!.Id, new ChatPermissions()
            //{
            //    CanAddWebPagePreviews = true,
            //    CanSendMessages = true,
            //    CanSendAudios = true,
            //    CanSendVideos = true,
            //    CanChangeInfo = true,
            //    CanInviteUsers = true,
            //    CanManageTopics = true,
            //    CanPinMessages = true,
            //    CanSendDocuments = true,
            //    CanSendOtherMessages = true,
            //    CanSendPhotos = true,
            //    CanSendPolls = true,
            //    CanSendVideoNotes = true,
            //    CanSendVoiceNotes = true,
            //}, useIndependentChatPermissions: true, untilDate: DateTime.Now.AddMinutes(1), cancellationToken: cancellationToken);

            ////此方法也可以解除禁言
            ////await botClient.PromoteChatMemberAsync(msg.Chat.Id, msg.From!.Id, canPostMessages: true);
        }
    }
}